package com.windoent.yugioh.ui.activity.meinfo;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.baidu.mobstat.StatService;
import com.windoent.yugioh.R;
import com.windoent.yugioh.base.BaseActivity;
import com.windoent.yugioh.utils.BaseMethod;
import com.windoent.yugioh.utils.EditLimitStringUtils;
import com.windoent.yugioh.utils.ToastUtils;
import com.windoent.yugioh.widget.ActivityTitle;

/**
 * author : robert
 * date   : 2021/5/10 15:44
 * desc   :修改昵称
 */
public class ModifyUserNickNameActivity extends BaseActivity {
    private ActivityTitle mActivityTitle;
    private EditText mEtName;
    private Button mBtnSave;
    private TextView mTvNick;
    private TextView mTvWarn;

    private boolean is_name;

    @Override
    public int getLayout() {
        return R.layout.activity_modify_nick_name;
    }

    @Override
    public void initView() {
        super.initView();
        mActivityTitle = (ActivityTitle) findViewById(R.id.activity_title);
        immersiveStatusBar(mActivityTitle.getView());
        mEtName = (EditText) findViewById(R.id.et_name);
        mBtnSave = (Button) findViewById(R.id.btn_save);

        mTvNick = (TextView) findViewById(R.id.tv_nick);
        mTvWarn = (TextView) findViewById(R.id.tv_warn);
    }

    @Override
    public void initData() {
        super.initData();
        Intent intent = getIntent();
        String name = intent.getStringExtra("name");
        is_name = intent.getBooleanExtra("is_name", true);
        mActivityTitle.setTvTitle(is_name ? getString(R.string.string_modify_nick_name) : getString(R.string.string_modify_signature));
        mTvNick.setText(is_name ? getString(R.string.string_name) : getString(R.string.string_signature));
        mTvWarn.setText(is_name ? getString(R.string.string_numbers_name_ten) : getString(R.string.string_numbers_name_fifth));
        mEtName.setHint(is_name ? getString(R.string.string_nick_name_ten_words) : getString(R.string.string_signature_fifth_words));
        mEtName.setText(name);
        EditLimitStringUtils.lengthFilter(mEtName, is_name ? 10 : 15, is_name ? getString(R.string.string_nick_name_ten_words) : getString(R.string.string_signature_fifth_words));
    }

    @Override
    public void initListener() {
        super.initListener();
        mActivityTitle.setBackListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mBtnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name = mEtName.getText().toString().trim();
                if (is_name) {
                    if (BaseMethod.isNull(name)) {
                        ToastUtils.showText(getString(R.string.string_name_null));
                    } else if (name.length() > 10) {
                        ToastUtils.showText(getString(R.string.string_nick_name_ten_words));
                    } else {
                        Intent intent = new Intent();
                        intent.putExtra("nick_name", name);
                        setResult(Activity.RESULT_OK, intent);
                        finish();
                    }
                } else {
                    if (BaseMethod.isNull(name)) {
                        ToastUtils.showText(getString(R.string.string_signature_null));
                    } else if (name.length() > 15) {
                        ToastUtils.showText(getString(R.string.string_signature_fifth_words));
                    } else {
                        Intent intent = new Intent();
                        intent.putExtra("nick_name", name);
                        setResult(Activity.RESULT_OK, intent);
                        finish();
                    }
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        StatService.onPageStart(this, is_name ? "修改昵称界面" : "修改签名界面");
    }

    @Override
    protected void onPause() {
        super.onPause();
        StatService.onPageEnd(this, is_name ? "修改昵称界面" : "修改签名界面");
    }
}
